package com.lucky.blog.service.impl;

import com.lucky.blog.domain.ArticleDO;
import com.lucky.common.config.SystemConfig;
import com.lucky.common.utils.CallCmd;
import com.lucky.common.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lucky.blog.dao.ContentDao;
import com.lucky.blog.domain.ContentDO;
import com.lucky.blog.service.ContentService;
import org.thymeleaf.context.Context;

@Service
public class ContentServiceImpl implements ContentService {
	@Autowired
	private ContentDao bContentMapper;
	@Autowired
	private SystemConfig config;

    @Override
	public ContentDO get(Long cid){
		return bContentMapper.get(cid);
	}
	
	@Override
	public List<ContentDO> list(Map<String, Object> map){
		return bContentMapper.list(map);
	}

	@Override
	public List<ArticleDO> homeList(Map<String, Object> map) {
		return bContentMapper.homeList(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return bContentMapper.count(map);
	}
	
	@Override
	public int save(ContentDO bContent){
        String[] split = bContent.getTags().split(",");
        Map<String, Object> relevantMap = new HashMap<>();
        relevantMap.put("tags", split[0]);
        relevantMap.put("offset", 0);
        relevantMap.put("limit", 7);
        int save = bContentMapper.save(bContent);
        List<ContentDO> relevantList = bContentMapper.list(relevantMap);
        Context context = new Context();
        context.setVariable("content", bContent);
        context.setVariable("relevantList", relevantList);
        String fileName = "detail_" + bContent.getCid() + ".html";
        FileUtil.generateFile(context, "detail", config.getGeneration() + "news/", fileName);
        if(config.isPush()){
            execCMD();
        }
        return save;
	}
	
	@Override
	public int update(ContentDO bContent){
        String[] split = bContent.getTags().split(",");
        Map<String, Object> relevantMap = new HashMap<>();
        relevantMap.put("tags", split[0]);
        relevantMap.put("offset", 0);
        relevantMap.put("limit", 7);
        int update = bContentMapper.update(bContent);
        List<ContentDO> relevantList = bContentMapper.list(relevantMap);
        Context context = new Context();
        context.setVariable("content", bContent);
        context.setVariable("relevantList", relevantList);
        String fileName = "detail_" + bContent.getCid() + ".html";
        FileUtil.generateFile(context, "detail", config.getGeneration() + "news/", fileName);
        if(config.isPush()){
            execCMD();
        }
        return update;
	}
	
	@Override
	public int remove(Long cid){
        String fileName = "detail_" + cid + ".html";
        String filePath = config.getGeneration() + "news/" + fileName;
        FileUtil.deleteFile(filePath);
		return bContentMapper.remove(cid);
	}
	
	@Override
	public int batchRemove(Long[] cids){
        for (int i = 0; i < cids.length; i++) {
            String fileName = "detail_" + cids[i] + ".html";
            String filePath = config.getGeneration() + "news/" + fileName;
            FileUtil.deleteFile(filePath);
        }
		return bContentMapper.batchRemove(cids);
	}

    @Override
    public void batchGenerate(String cids) {
        Map<String, Object> map = new HashMap<>();
        map.put("cids", cids);
        List<ContentDO> list = bContentMapper.list(map);
        for (ContentDO bContent : list) {
            Context context = new Context();
            String[] split = bContent.getTags().split(",");
            Map<String, Object> relevantMap = new HashMap<>();
            relevantMap.put("tags", split[0]);
            relevantMap.put("offset", 0);
            relevantMap.put("limit", 7);
            List<ContentDO> relevantList = bContentMapper.list(relevantMap);
            context.setVariable("content", bContent);
            context.setVariable("relevantList", relevantList);
            String fileName = "detail_" + bContent.getCid() + ".html";
            FileUtil.generateFile(context, "detail", config.getGeneration() + "news/", fileName);
        }
        if(config.isPush()){
            execCMD();
        }
    }

    @Override
    public void execCMD(){
        String path = "F:\\data\\lucky-blog\\blog\\gitPush.bat";
        CallCmd.exec(path);
    }
}
